<?php

/**
 * libretaentrega actions.
 *
 * @package    courier
 * @subpackage libretaentrega
 * @author     Your name here
 */
class libretaentregaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->Libretaentregas = LibretaentregaPeer::doSelect(new Criteria());
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->Libretaentrega = LibretaentregaPeer::retrieveByPk($request->getParameter('len_id'));
    $this->forward404Unless($this->Libretaentrega);
  }

 public function executeShowDireccionentrega(sfWebRequest $request)
  {
    $this->Cliente = ClientePeer::retrieveByPk($request->getParameter('cli_id'));
    $this->forward404Unless($this->Cliente);
  }

  public function executeNew(sfWebRequest $request)
  {
	if ($request->getParameter('cli_id') != null){
		$identificadorCliente = new Libretaentrega();
		$identificadorCliente->setLenCliente($request->getParameter('cli_id'));
		$this->form = new LibretaentregaForm($identificadorCliente);
	}
	else if ($request->getParameter('sol_cliente') != null){
		$idClienteSolicitud = new Libretaentrega();
		$idClienteSolicitud->setLenCliente($request->getParameter('sol_cliente'));
		$this->form = new LibretaentregaForm($idClienteSolicitud);
	}
	else{
		$this->form = new LibretaentregaForm();
	}
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new LibretaentregaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Libretaentrega = LibretaentregaPeer::retrieveByPk($request->getParameter('len_id')), sprintf('Object Libretaentrega does not exist (%s).', $request->getParameter('len_id')));
    $this->form = new LibretaentregaForm($Libretaentrega);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Libretaentrega = LibretaentregaPeer::retrieveByPk($request->getParameter('len_id')), sprintf('Object Libretaentrega does not exist (%s).', $request->getParameter('len_id')));
    $this->form = new LibretaentregaForm($Libretaentrega);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Libretaentrega = LibretaentregaPeer::retrieveByPk($request->getParameter('len_id')), sprintf('Object Libretaentrega does not exist (%s).', $request->getParameter('len_id')));
    $Libretaentrega->delete();

    $this->redirect('libretaentrega/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
	$Libretaentrega = $form->save();
	$this->redirect('libretaentrega/showDireccionentrega?cli_id='.$request->getParameter('libretaentrega[len_cliente]'));
    }
  }
}
